﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace Xwork.SEO.Providers
{
    public abstract class SearchEngineProvider
    {
        protected abstract string GetQueryUrl(string keyword);
        protected abstract SearchResult GetResult(string html,string site);
        public Task<SearchResult> DoSearch(string keyword, string site)
        {
            string url = GetQueryUrl(keyword);
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

            return client.GetStringAsync(url).ContinueWith<SearchResult>(p => GetResult(p.Result, site));
        }


        public static SearchEngineProvider GetProvider(string providerName)
        {
            return (SearchEngineProvider)System.Activator.CreateInstance(Type.GetType("Xwork.SEO.Providers." + providerName));
        }
    }
}
